package security;

import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  

/** 
 * 功能简介：MD5加密工具类 
 * 密码等安全信息存入数据库时，转换成MD5加密形式 
 * @author  
 *  
 * 
 */  
public class EnCriptUtil  
{  
	//encriptType可以选用MD5和SHA-1
    public static String getEcriptStr(String inStr,String encriptType)  
    {  
        String outStr=null;  
        if(inStr ==null)  
        {  
            outStr = null;  
        }  
        else if("".equals(inStr))  
        {  
            outStr = "";  
        }  
        else  
        {  
            try  
            {  
                MessageDigest md = MessageDigest.getInstance(encriptType);  
                md.update(inStr.getBytes());  
                byte b[] = md.digest();  
                StringBuffer buf = new StringBuffer();  
                for(int i=0;i<b.length;i++)  
                {  
                    int c = b[i]>>>4 & 0xf;  
                    buf.append(Integer.toHexString(c));  
                    c = b[i]& 0xf;  
                    buf.append(Integer.toHexString(c));  
                }  
                outStr = buf.toString();  
            }  
            catch (NoSuchAlgorithmException e)  
            {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
        return outStr;  
    } 
    
    //用于生成对用户密码进行md5加密的字符串结构，使用用户名+关键字+密码
    public static String fix(String str1,String str2){
    	return str1 + "ac" + SystemParameter.key + "bd" + str2 + "60";
    }
    
    public static void main(String args[]){
    	//String str = "123456";
    	String str = fix("zhangjs","123456");
    	String s = EnCriptUtil.getEcriptStr(str,"MD5");
    	System.out.println(s);
    	System.out.println(s.length()*4);
    	//2c415067d31c71e2dd364ae173c78a
    }
}  